package com.hhss.qishi.mapper.provider;

import com.hhss.qishi.entity.Picture;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

/**
 * @Auther: zcc
 * @Date: 19-6-20 15:00
 * @Description: 图片SQL构造器
 */
public class PictureProvider {

    private static String SQL_ENTITY = "id, uuid,"
            + " picture_name AS pictureName, theme, hits, is_public AS isPublic, is_topping AS isTopping,"
            + " summary, remark,"
            + " status, create_user AS createUser, create_time AS createTime, last_update_user AS lastUpdateUser,"
            + " last_update_time AS lastUpdateTime, create_user AS createUser, last_update_user AS lastUpdateUser";

    private static String TABLE_NAME = "tb_picture";

    /**
     * 查找照片集合
     * @param picture
     * @return
     */
    public String getListSQL(final Picture picture) {
        return new SQL(){{
            SELECT(SQL_ENTITY);
            FROM(TABLE_NAME);
            WHERE("1=1");
            if(!StringUtils.isEmpty(picture.getPictureName())) {
                AND().WHERE("picture_name like CONCAT('%',#{picture.pictureName},'%')");
            }
            if(!StringUtils.isEmpty(picture.getTheme())) {
                AND().WHERE("theme = #{picture.theme}");
            }
            if(!StringUtils.isEmpty(picture.getIsPublic())) {
                AND().WHERE("is_public = #{picture.isPublic}");
            }
            ORDER_BY("last_update_time DESC");
        }}.toString();
    }
}
